package fm.dongman.animefm.view.activity

import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import fm.dongman.animefm.R
import fm.dongman.animefm.view.adapter.FindPagerAdapter
import fm.dongman.animefm.view.fragment.*
import fm.dongman.animefm.view.util.ActivityHelper
import fm.dongman.animefm.view.util.ShareHelper
import fm.dongman.animefm.view.util.ToastHelper
import kotlinx.android.synthetic.main.activity_index.*

class IndexActivity : BaseActivity() {
    private lateinit var mNavTitle: List<String> // 导航标题
    private lateinit var mFragments: Array<BaseFragment> // 导航碎片
    private var mLastBackTime:Long = 0 // 最后点击返回按钮时间

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_index)
        initData()
        bindView()
    }

    override fun onResume() {
        super.onResume()
        checkClipShareData()
    }

    /**
     *检查截切板是否存在分享数据
     */
    private fun checkClipShareData() {
        ShareHelper.checkClipBoardIsExistShare(this, ShareHelper.OnFoundShareInfoListener { type, id ->
            var strType = ""
            var openType = ""

            when (type) {
                ShareHelper.TYPE_SHEET -> {
                    strType = "漫单"
                    openType = SingleActivity.INTENT_EXTRA_SHEET_INFO
                }
                ShareHelper.TYPE_SPECIAL -> {
                    strType = "专题"
                    openType = SingleActivity.INTENT_EXTRA_SPECIAL_INFO
                }
                ShareHelper.TYPE_COMIC -> {
                    strType = "动漫"
                    openType = SingleActivity.INTENT_EXTRA_ANIME_INFO
                }
            }

            AlertDialog.Builder(this)
                    .setTitle("来自剪切板的分享数据")
                    .setMessage("检测到" + strType + "分享数据,是否打开?")
                    .setNegativeButton("取消", DialogInterface.OnClickListener { dialog, _ ->
                        dialog.dismiss()
                    })
                    .setPositiveButton("打开", DialogInterface.OnClickListener { _, _ ->
                        val intent = Intent(this, SingleActivity::class.java)
                        intent.putExtra(SingleActivity.EXTRA_NAME, openType)
                        intent.putExtra(SingleActivity.BUNDLE_EXTRA_KEY_ID, id)
                        ActivityHelper.startActivity(this, intent)
                    }).show()
        })
    }

    /**
     * 初始化数据
     */
    private fun initData() {
        initBottomNavData()
    }

    /**
     * 初始化底部导航栏数据
     */
    private fun initBottomNavData() {
        mNavTitle = listOf(getString(R.string.str_home), getString(R.string.str_find),
                getString(R.string.str_comic), getString(R.string.str_my))
        mFragments = arrayOf(HomeFragment.getInstance(), FindFragment.getInstance(),
                ComicFragment.getInstance(), MyFragment.getInstance())
    }

    /**
     * 绑定数据到 View
     */
    private fun bindView() {
        bindPager()
        bindBottomNav()
    }

    /**
     * 绑定 ViewPager
     */
    private fun bindPager() {
        mHomeViewPager.adapter = FindPagerAdapter(fragmentManager, arrayOf(
                getString(R.string.str_find_article), getString(R.string.str_find_article),
                getString(R.string.str_find_article)), mFragments)
        mHomeViewPager.offscreenPageLimit = 3
    }

    /**
     * 绑定底部导航栏
     */
    private fun bindBottomNav() {
        mBottomNav.setMode(BottomNavigationBar.MODE_FIXED)
        mBottomNav.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
        mBottomNav.addItem(BottomNavigationItem(R.drawable.ic_home_selected, mNavTitle[0]).setInactiveIconResource(R.drawable.ic_home_unselected))
                .addItem(BottomNavigationItem(R.drawable.ic_discover_selected, mNavTitle[1]).setInactiveIconResource(R.drawable.ic_discover_unselected))
                .addItem(BottomNavigationItem(R.drawable.ic_comic_frame_selected, mNavTitle[2]).setInactiveIconResource(R.drawable.ic_comic_frame_unselected))
                .addItem(BottomNavigationItem(R.drawable.ic_mine_selected, mNavTitle[3]).setInactiveIconResource(R.drawable.ic_mine_unselected))
                .setActiveColor(R.color.primary)
                .setInActiveColor(R.color.nav_icon_unselected_color)
                .setFirstSelectedPosition(0).initialise()

        mBottomNav.setTabSelectedListener(mBottomNavSelectedListener)
        mHomeViewPager.currentItem = 0
    }

    /**
     * 底部导航选中事件监听器
     */
    private val mBottomNavSelectedListener = object : BottomNavigationBar.OnTabSelectedListener {
        override fun onTabSelected(position: Int) {
            mHomeViewPager.setCurrentItem(position, false)
        }

        override fun onTabUnselected(position: Int) {}

        override fun onTabReselected(position: Int) {}
    }

    override fun onBackPressed() {
        if (checkDoubleBackTap()){
            super.onBackPressed()
        }
    }

    /**
     * 检测是否双击了返回
     */
    private fun checkDoubleBackTap(): Boolean {
        if (System.currentTimeMillis() - mLastBackTime > 2000) {
            mLastBackTime = System.currentTimeMillis()
            ToastHelper.getInstance(this).showWarn("确定要退出吗？")
            return false
        }
        return true
    }

    override fun onDestroy() {
        super.onDestroy()
        // 销毁所有的 Fragment
        destroyFragment()
    }
}
